﻿using Basic.FlowGatewayModule.Interface;
using Basic.FlowGatewayModule.Model;
using Basic.FlowRemoteModel;
using Basic.FlowRemoteModel.FlowApp;
using Basic.FlowRemoteModel.FlowApp.Model;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.HttpApiGateway.Model;

namespace Basic.FlowGatewayModule.lmpl
{
    internal class FlowAppService : IFlowAppService
    {
        private readonly ITreeDicService _TreeDic;
        private static readonly int _FlowType = 10;
        public FlowAppService ( ITreeDicService dic )
        {
            this._TreeDic = dic;
        }

        public long Add ( FlowAppSet data )
        {
            return new AddFlowApp
            {
                Datum = data
            }.Send();
        }

        public bool Enable ( long id )
        {
            return new EnableFlowApp
            {
                Id = id
            }.Send();
        }

        public FlowAppBase Get ( long id )
        {
            return new GetFlowApp
            {
                Id = id
            }.Send();
        }

        public FlowApp[] GetList ( FlowAppType? appType )
        {
            FlowAppDatum[] list = new GetAppList
            {
                AppType = appType
            }.Send();
            if ( list.IsNull() )
            {
                return null;
            }
            string[] vals = list.Distinct(a => a.AppClassify);
            Dictionary<string, string> dic = this._TreeDic.GetNames(_FlowType, vals);
            return vals.ConvertAll(a =>
            {
                return new FlowApp
                {
                    Text = dic[a],
                    Value = a,
                    AppList = list.Convert(c => c.AppClassify == a, c => new FlowAppUI
                    {
                        AppName = c.AppName,
                        AppShow = c.AppShow,
                        AppType = c.AppType,
                        Id = c.Id
                    })
                };
            });
        }

        public PagingResult<FlowAppData> Query ( PagingParam<FlowAppQuery> param )
        {
            return new QueryFlowApp
            {
                Query = param.Query,
                Size = param.Size,
                IsDesc = param.IsDesc,
                SortName = param.SortName,
                NextId = param.NextId,
                Index = param.Index,
            }.Send();
        }

        public bool Set ( long id, FlowAppSet set )
        {
            return new SetFlowApp
            {
                Id = id,
                Datum = set
            }.Send();
        }

        public bool SetFlow ( long id, AppFlowSet set )
        {
            return new SetAppFlow
            {
                Id = id,
                Data = set
            }.Send();
        }

        public bool Stop ( long id )
        {
            return new StopFlowApp
            {
                Id = id
            }.Send();
        }

        public bool SetSettleFormId ( long id, long? formId )
        {
            return new SetAppSettleForm
            {
                AppId = id,
                FormId = formId
            }.Send();
        }

        public void Delete(long id)
        {
            new DeleteFlowApp
            {
                Id = id,
            }.Send();
        }
    }
}
